package com.league.zjxn.main.home

import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.SearchViewModel
import com.league.zjxn.main.adapter.HomeSearchResultAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import kotlinx.android.synthetic.main.fragment_search_result.*

class SearchResultFragment : OwnerViewModeFragment<SearchViewModel>() {

    companion object {
        fun newInstance(): SearchResultFragment {
            return SearchResultFragment()
        }
    }

    private val mHomeSearchResultAdapter = HomeSearchResultAdapter()

    override fun viewModel(): SearchViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_search_result

    override fun initView() {
        rvSearchResultBody.adapter = mHomeSearchResultAdapter.apply {
            setOnItemClickListener { _, _, position ->
                val itemData = getItem(position)
                TrendDetailsActivity.openCon2(homeSearch = itemData)
            }
        }
    }

    override fun initViewMode() {
        viewModel.homeSearchLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data?.data
                ?: return@observe
            mHomeSearchResultAdapter.setNewInstance(data)
        })
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mHomeSearchResultAdapter.data.clear()
        mHomeSearchResultAdapter.notifyDataSetChanged()
    }

}